from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from collections import OrderedDict


def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'token': token,
        'username': user.username,
        'user_id': user.id,
    }


# 分页
class MyPageNumberPagination(PageNumberPagination):
    page_size = 3  # 设置每页默认显示数据条数
    page_size_query_param = 'pagesize'  # 请求过来的指定的显示数据条数
    max_page_size = 5  # 设置每页最大数据条数

    # 重写get_paginated_response方法,返回需要的数据
    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('page', self.page.number),     # 当前页数
            ('pages', self.page.paginator.num_pages),       # 总页数
            ('lists', data)     # 每一个用户对象
        ]))
